$(function(){
    layui.use('layer', function(){
        // layer.msg('接口异常',{icon: 2});
        layer.config({
            skin: 'demo-class'
          })
    })
    //页面加载完成之后执行
    $('.layui-nav>li').click(function(){
        var name = $(this).data("link")
        var flag = 1
        if(name == 'restdataclient' || name == 'configureList') {
            flag = 0
        } else {
            flag = 1
        }
        if(name === 'update') {
            layui.use(['form', 'layer'], function(){
                var layer = layui.layer;
                var form = layui.form;
                form.verify({
                    originalPassword: [
                        /^[\S]{6,12}$/
                        ,'密码必须6到12位，且不能出现空格'
                      ]
                    ,newPassword: [
                      /^[\S]{6,12}$/
                      ,'密码必须6到12位，且不能出现空格'
                    ]
                    ,confirmPassword: function(value) {
                        var data = form.val('form');
                        if(value !== data.newPassword) {
                            return '您输入的密码不一致'
                        }
                    }
                  });
                layer.open({
                    type: 1,
                    title:'修改密码',
                    area:['325px'],
                    // btnAlign: 'c',
                    // btn:['确定','取消'],
                    yes:function(){
                    },
                    cancel : function(){
                        form.val('form', {
                            originalPassword: '',
                            newPassword: '',
                            confirmPassword: ''
                        })
                    },
                    content: $('#updatepassword') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
               
                // layui.$('#submit').on('click', function(){
                //     var data = form.val('form');
                // });

                form.on('submit(submit)', function(data){
                    console.log(data.field);
                    data.field.flag = flag
                    $.ajax({
                        contentType: 'application/json',
                        type: 'post',
                        url: '/api/chx/user/resetpassword',
                        data:JSON.stringify(data.field),
                        success: function(res) {
                            var val = 5
                            if (res.data === '修改密码成功') {
                                val = 6
                            } else if(res.data === '输入的旧密码不正确'){
                                val = 5
                            } 
                            form.val('form', {
                                originalPassword: '',
                                newPassword: '',
                                confirmPassword: ''
                            })
                            layer.closeAll()
                            layer.msg(res.data,{icon: val});
                        },
                        error:function(a,b,c){
                            layer.msg('接口异常',{icon: 2});
                        }
                    })
                    return
                });
            
            });
        } else {
             // window.location.href =  '/chx/' + name;
            window.location.href =  '/' + name+'.html';
        }
        
    })
});